package com.biqee.service.bq.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;

import com.biqee.dao.BqQueryRelationDAO;
import com.biqee.dao.BqTuanDAO;
import com.biqee.exception.BusinessUnCheckedException;
import com.biqee.model.TaobaoParseReulst;
import com.biqee.model.TbQueryModel;
import com.biqee.model.common.BqTuanStatus;
import com.biqee.model.common.PageInfoObject;
import com.biqee.model.db.BqQuery;
import com.biqee.model.db.BqTuan;
import com.biqee.model.db.TbItem;
import com.biqee.service.bq.BqQueryService;
import com.biqee.service.bq.BqTuanService;
import com.biqee.service.tb.TaobaoParseService;
import com.biqee.util.SegmentUtil;

public class BqTuanServiceImpl implements BqTuanService {
	private static final Logger LOG_KEYWORDS = Logger.getLogger("keywords");
	
	private BqQueryRelationDAO bqQueryRelationDAO;
	
	private BqQueryService bqQueryService;
	
	@Override
	public List<BqTuan> listGoodTuanOfToday(int start, int count) {
		return bqTuanDAO.listGoodTuanOfToday(start, count);
	}

	@Override
	public List<BqTuan> listCommonTuanOfToday(int start, int count) {
		return bqTuanDAO.listCommonTuanOfToday(start, count);
	}

	@Override
	public List<BqTuan> listBadTuanOfToday(int start, int count) {
		return bqTuanDAO.listBadTuanOfToday(start, count);
	}

	private Integer create(BqTuan bqTuan) {
		return bqTuanDAO.create(bqTuan);
	}

	@Override
	public Integer update(BqTuan bqTuan) {
		return bqTuanDAO.update(bqTuan);
	}
	
	@Override
	public Integer updateSelective(BqTuan bqTuan) {
		return bqTuanDAO.updateSelective(bqTuan);
	}
	
	@Override
	public int save(BqTuan bqTuan) {
		BqTuan bqTuanInDb = readBySiteItem(bqTuan.getSite(), bqTuan.getSiteItemId());
		if(bqTuanInDb == null) {
			return create(bqTuan);
		} else {
			Integer id = bqTuanInDb.getId();
			Date gmtCreate = bqTuanInDb.getGmtCreate();
			try {
				BeanUtils.copyProperties(bqTuanInDb, bqTuan);
			} catch (Exception e) {
				// ignore
			}

			bqTuanInDb.setGmtCreate(gmtCreate);
			bqTuanInDb.setId(id);
			
			return id;
		}
	}

	@Override
	public BqTuan readBySiteItem(String site, String siteItemId) {
		return bqTuanDAO.readBySiteItem(site, siteItemId);
	}
	
	@Override
	public BqTuan getById(Integer tuanId){
		return bqTuanDAO.read(tuanId);
	}

	@Override
	public List<BqTuan> listTuanByPage(PageInfoObject page) {
		int count = countTuan();
		page.setCount(count);
		return bqTuanDAO.listTuanByPage(page);
	}

	@Override
	public void search(BqTuan bqTuan) {
		if(BqTuan.SITE_TAOBAO.equals(bqTuan.getSite())){
			
			TaobaoParseReulst taobaoParseReulst = taobaoParseService.parse(bqTuan.getSiteItemId());
			if(taobaoParseReulst==null||taobaoParseReulst.getTbItem()==null){
				throw new BusinessUnCheckedException("error parse item. id=" + bqTuan.getSiteItemId());
			}
			save(bqTuan);
			BqQuery q = buildBqQuery(taobaoParseReulst.getTbItem());
			q.setTuanId(bqTuan.getId());
			TbQueryModel bqQuery= new TbQueryModel();
			bqQuery.setBqQuery(q);
			int count = bqQueryService.searchTbItems(bqQuery);
			LOG_KEYWORDS.info("tuanId="+bqTuan.getId()+"listname="+bqQuery.buildFileName()+":count="+count+";q="+q.getQueryQ());
			this.tuanSummary(bqTuan.getId());
		}
		
	}
	
	@Override
	public void delete(Integer tuanId) {
		bqQueryService.deletedByTuanId(tuanId);
		bqTuanDAO.delete(tuanId);
	}
	
	@Override
	public List<BqTuan> listUnEstimatedTuans(int start,int number) {
		return bqTuanDAO.listNullStatusRecords(start, number);
	}

	@Override
	public void updateStatus(BqTuanStatus status, Integer id) {
		bqTuanDAO.updateStatus(status, id);
	}

	
	@Override
	public int countStatus(BqTuanStatus status) {
		return bqTuanDAO.countStatus(status);
	}
	
	@Override
	public List<String> listAllUnExpiredJuHuaSuanItemIds() {
		return bqTuanDAO.listAllUnExpiredJuHuaSuanItemIds();
	}

	private BqQuery buildBqQuery(TbItem tbItem){
		
			 BqQuery q = new BqQuery();
		q.setTuanTitle(tbItem.getTitle());
		q.setQueryQ(SegmentUtil.splitWords(tbItem.getTitle()));
		q.setQueryParams("&cat="+tbItem.getTbCatId());
		q.setType(BqQuery.TYPE_AUTO);
		return q ;
	}
	
	
	private BqTuanDAO bqTuanDAO;
	
	
	private TaobaoParseService taobaoParseService;

	public void setBqTuanDAO(BqTuanDAO bqTuanDAO) {
		this.bqTuanDAO = bqTuanDAO;
	}
	
	public void setBqQueryService(BqQueryService bqQueryService) {
		this.bqQueryService = bqQueryService;
	}

	public void setTaobaoParseService(TaobaoParseService taobaoParseService) {
		this.taobaoParseService = taobaoParseService;
	}
	
	public int countTuan(){
		return bqTuanDAO.countAll();
	}

	@Override
	public void tuanSummary(Integer tuanId) {
		Map<Object,Object> condition = new HashMap<Object,Object>();
		condition.put("tuanId", tuanId);
		int count = bqQueryRelationDAO.countRelationItems(condition);
		BqTuan bqTuan = new BqTuan();
		bqTuan.setId(tuanId);
		bqTuan.setTotalItem(count);
		this.updateSelective(bqTuan);
		
	}

	public void setBqQueryRelationDAO(BqQueryRelationDAO bqQueryRelationDAO) {
		this.bqQueryRelationDAO = bqQueryRelationDAO;
	}
	
	
}
